//引入字体图标样式
require("../../assets/fonts/iconfont.css");
//移入页面的css样式
require("./login.less");
//引入axios
const axios = require("axios");
//引入自定义js
let obj = require("../../assets/js/my.js");
let $ = obj.$;
let local = obj.local;
window.addEventListener("load", function () {
	// 先清空所有的本地缓存
	local.clear();
	//账号注册
	$("header span").addEventListener("click", function () {
		location.href = "./register.html";
	});
	$("#btn").addEventListener("click", async function () {
		//账号验证
		let reg =
			/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|17[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/;
		if (reg.test($("#tell").value)) {
			$("#error").textContent = "";
			$(".icon-tishicuowu").style.display = "none";
		} else {
			$("#error").textContent = "手机号格式错误";
			$(".icon-tishicuowu").style.display = "inline";
			return;
		}
		//密码
		let reg1 = /^\w{6,18}$/;
		if (reg1.test($("#psw").value)) {
			$("#error").textContent = "";
			$(".icon-tishicuowu").style.display = "none";
		} else {
			$("#error").textContent = "请输入6-18位(字母、数字、下划线)";
			$(".icon-tishicuowu").style.display = "inline";

			return;
		}
		//判断账号是否存在
		let ss = await axios.post(
			"http://fitness.h5.itsource.cn:3701/api/user/isExist",
			{
				account: $("#tell").value,
			}
		);
		if (ss.data.errno !== 10002) {
			$("#error").textContent = "账号未注册";
			return;
		}
		let res = await axios.post(
			"http://fitness.h5.itsource.cn:3701/api/user/login",
			{
				account: $("#tell").value,
				password: $("#psw").value,
			}
		);

		if (res.data.errno === 0) {
			local.setItem("token", res.data.data.token);
			location.href = "./index.html";
		} else {
			$("#error").textContent = "密码输入错误";
			return;
		}
	});
});
